package com.yipinhouse.signing.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

public class OssUtil {
    static String id = "LTAI5tRH68kyhni6Ee4X3kjb";
    static String secret = "o12qIB1XBTagIy930obzG01JXpM0Rz";
    static String endPoint = "oss-cn-chengdu.aliyuncs.com";
    //创建客户端对象

    /**
     * 上传
     * @param bucketName
     * @param fileName
     * @param file
     * @return
     * @throws Exception
     */
    public static String uploadFile(String bucketName, String fileName, InputStream file) throws Exception {
        OSS ossClient = new OSSClientBuilder().build("http://"+endPoint, id, secret);
        try {
            //需要上传的文件和指定分组的储存空间
            ossClient.putObject(bucketName, fileName, file);
            return "http://"+bucketName+"."+endPoint+"/"+fileName;
        } finally {
            if(ossClient != null) ossClient.shutdown();
        }
    }

    /**
     * 删除文件
     * @param bucketName
     * @param fileName
     * @return
     */
    public static boolean removeFile(String bucketName,String fileName) {
        OSS ossClient = new OSSClientBuilder().build("http://"+endPoint, id, secret);
        try {
            ossClient.deleteObject(bucketName, fileName);
            return true;
        } finally {
            if(ossClient != null) ossClient.shutdown();
        }
    }
}
